<?php
namespace app\admin\controller;
use think\Db;
class Index
{
	public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {
		$menu = array();
		$data = Db::table("webconfig")->where(['id'=>1])->find();
		$data['system'] = php_uname('s');
		$topmenu = Db::table("admin_menu")->where('gid',0)->whereIn('auth',session('auth'))->select();
		if(!empty($topmenu)){
			foreach($topmenu as $sub){
				$sub['sub'] = Db::table("admin_menu")->where('gid',$sub['id'])->whereIn('auth',session('auth'))->select();
				Array_push($menu,$sub);
			}
		}
        return view('',['data'  => $data,'menu'=>$menu]);
    }
	
	public function home()
    {
        if(request()->request('service_phone')){
            $pid = request()->request('pid');
            $cid = request()->request('cid');
            $aid = request()->request('aid');
            $count_service_phone = Db::table('service_phone')->where('aid',$aid)->count();
            if($count_service_phone == 0){
                Db::table('service_phone')->insert(['aid'=>$aid,'service_phone'=>'']);
            }else{
                Db::table('service_phone')->where('aid',$aid)->update(['service_phone'=>request()->request('service_phone')]);
            }
            redirect(url('index/home').'?pid='.$pid.'&cid='.$cid.'&aid='.$aid);
            exit;
        }else{
            $service_phone = '';
            $where = [];
            $aid = request()->request('aid');
            if(!empty($aid)){
                $where['aid'] = $aid;
                $count_service_phone = Db::table('service_phone')->where('aid',$aid)->count();
                if($count_service_phone == 0){
                    Db::table('service_phone')->insert(['aid'=>$aid,'service_phone'=>'']);
                }
                $service_phone = Db::table('service_phone')->where('aid',$aid)->value('service_phone');
            }
            $beginToday = strtotime(date('Y-m-d'));
            $endToday = $beginToday+86400;
            $data['sum_order_count'] = Db::table("order")->whereIn('aid',session('aid'))->where($where)->count();
            $data['today_order_count'] = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['time'=>['between',$beginToday.','.$endToday]])->count();
            $data['express_total'] = Db::table("express")->whereIn('aid',session('aid'))->where($where)->count();
            $system_money = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['time'=>['between',$beginToday.','.$endToday]])->select();
            $money_total = 0;
            foreach ($system_money as $k => $v){
                $money_total += $v['system_money'];
            }
            $data['money_total'] = $money_total;
            $today_begin_time = $beginToday;
            $today_end_time = $endToday;
            $date_data_array = [];
            $date_string = '';
            for($i=7;$i>0;$i--){
                if($i != 7){
                    $date_string .= date('Y-m-d', strtotime('-'.$i.' days')).',';
                }
                $date_data_array[] = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['time'=>['between',$today_begin_time.','.$today_end_time]])->count();
                $today_begin_time -= 86400;
                $today_end_time -= 86400;
            }
            $date_array = explode(',',$date_string);
            $date_money_total_string = '';
            foreach ($date_array as $k => $v){
                if($v){
                    $today_begin_time = strtotime($v);
                    $today_end_time = $today_begin_time+86400;
                    $system_money = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['time'=>['between',$today_begin_time.','.$today_end_time]])->select();
                    $money_total = 0;
                    foreach ($system_money as $k2 => $v2){
                        $money_total += $v2['system_money'];
                    }
                    $date_money_total_string .= '{value:"'.$money_total.'",name:"'.$v.'"},';
                }

            }
            $date_money_total_string .= '{value:"'.$data['money_total'].'",name:"'.date('Y-m-d').'"},';
            $date_money_total_string = substr($date_money_total_string,0,strlen($date_money_total_string)-1);
            $date_string .= date('m-d');
            $date_data = implode(',',array_reverse($date_data_array));
            return view('',['data'=>$data,'service_phone'=>$service_phone,'date_array'=>$date_string,'date_data'=>$date_data,'date_money_total_string'=>$date_money_total_string]);
        }
    }
	
	public function setting()
	{
		$tab = input("get.tab");
		if(request()->isPost()){
			$data = input("post.");
			$edit = $data['edit'];
			unset($data['edit']);
			if($edit == ''){
        Db::table("setting")->where(['id'=>1])->update($data);
			}else{
				$type = $data['type'];
				if(!is_dir(ROOT_PATH.'/version/')){
						mkdir(ROOT_PATH.'/version/');
				}
				$file = file_get_contents($_FILES['apk']['tmp_name']);
				$name = $type.str_replace('.','_',$data['version']).'.apk';
				file_put_contents(ROOT_PATH.'/version/'.$name,$file);
				$data['time'] = time();
				$data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/version/'.$name;
				$data['size'] = filesize($_FILES['apk']['tmp_name']);
				Db::table('update')->insert($data);
			}
		}
		$data = Db::table("setting")->where(['id'=>1])->find();
		return view('',['data' => $data,'tab' => $tab]);
	}

}
